
########################################################################
#      File Name:	Figure 1-4 Network Plots.R				                   #
#      Date:   	June 2025     			            				               #
#      Author: 	Juan Acevedo-Ossa		                        	         #
#      Purpose:   Replication file for "The Indirect Effects of	       #
#				          Structural Power										                 #
#      Input Files:  gvc_trade_dyad.csv                           	   #	
#      Output File: "Figure 1" "Figure 2" "Figure 3" "Figure 4"        #
########################################################################



#1. INSTALL NECESSARY PACKAGES

# Call (install) library Netify
#if (!require(devtools)) {
#  install.packages("devtools")
#}
#library(devtools)

#install_github("netify-dev/netify")
#install.packages("ggplot2")
#install.packages("igraph")
#install.packages("gridExtra")


library(ggplot2)
library(netify)
library(igraph)
library(ggraph)
library(gridExtra)



rm(list=ls())

#Replace for the location of your folder

path <- "C:/Users/juanp/OneDrive - City University of New York/The Indirect Effects of Structural Power"



setwd(path)

#Read data
data <- read.csv(paste0(path,"/data/gvc_trade_dyad.csv"))




###########################################################################
#                                                                         #
#                              FIGURE 1                                   #   
#                     Regional flows GVC 2000-2015                        #
#                                                                         #
###########################################################################


#Create Netify object by regions

netifyregion <- netify(data, actor1='regiona', actor2 ='regionb',weight='gvc', time='t', symmetric=FALSE,
                       actor_time_uniform = TRUE, diag_to_NA = TRUE,missing_to_zero = TRUE, sum_dyads=TRUE)

#Subset years 1990, 1999, 2004, 2009, 2014

netifyregionsub <- subset_netlet(netifyregion, when_to_subset = c(1,10,15,20,25))

#Add centrality numbers

netifyregionsub <- add_nodal(
  netifyregionsub,
  summary_actor(netifyregionsub),
  actor='actor', time='time'
)


figure1 <- plot(netifyregionsub,
     edge_color='navyblue',
     point_color = 'gray',
     point_size_var='authority_score',
     layout ='nicely', 
     static_actor_positions=TRUE,
     remove_isolates=TRUE,
     add_label=FALSE,
     add_text=TRUE)

figure1

ggsave("Images/Figure 1.jpg",
       plot = figure1,
       dpi = 300,
       width = 8.18,
       height = 5.25,
       units = "in",
       device = "jpeg")







###########################################################################
#                                                                         #
#                             FIGURE 2A                                   #
#            Participation of value added in GVC 2000-2015                #
#                                                                         #
###########################################################################

#Convert to Netify object
netifygvc <- netify(data, actor1='exp', actor2 ='imp',weight='gvc', time='t', symmetric=FALSE,
                     actor_time_uniform = TRUE, diag_to_NA = TRUE,missing_to_zero = TRUE,
                     nodal_vars = c('gtrade','gtrade_fin','gtrade_int','traditional_trade','traditional_trade_int','traditional_trade_fin',
                                    'gvc','gvcbp','gvcfp','gvcmix',"alignment_us_important","alignment_us_ch_different" ))


netifygvc <- subset_netlet(netifygvc, when_to_subset = c(1,5,10,15,20,25))

# Add centrality variables
netifygvc = add_nodal(
  netifygvc,
  summary_actor(netifygvc),
  actor='actor',
  time='time'
)


#Highlight some countries
selected <- c("JPN","FRA","GBR","CHN","NLD", "DEU","USA")

#Create a DF for plotting
actor_stats <- summary_actor(netifygvc)

#Rename columns to get
colnames(actor_stats)[colnames(actor_stats) == "strength_sum_out"] <- "GVC Exports"


colnames(actor_stats)[colnames(actor_stats) == "network_share_total"] <- "GVC share"


figure2a <- plot_actor_stats(
  actor_stats, 
  across_actor = FALSE, 
  specific_actors = selected,
  specific_stats = c('GVC Exports'),
  multilayer = FALSE
  
)

figure2a
ggsave("Images/Figure 2A.jpg",
       plot = figure2a,
       dpi = 300,
       width = 4,
       height = 3.19,
       units = "in",
       device = "jpeg")




###########################################################################
#                                                                         #
#                             FIGURE 2B                                   #   
#        Participation of value added in GVC (Backward) 2000-2015         #
#                                                                         #
###########################################################################


netifygvcbp <- netify(data, actor1='exp', actor2 ='imp',weight='gvcbp', time='t', symmetric=FALSE,
                      actor_time_uniform = TRUE, diag_to_NA = TRUE,missing_to_zero = TRUE,
                      nodal_vars = c('gtrade','gtrade_fin','gtrade_int','traditional_trade','traditional_trade_int','traditional_trade_fin',
                                     'gvc','gvcbp','gvcfp','gvcmix'))



netifygvcbp <- subset_netlet(netifygvcbp, when_to_subset = c(1,5,10,15,20,25))

#Highlight some countries
selected <- c("JPN","FRA","GBR","CHN","NLD", "DEU","USA")

#Create a DF for plotting
actor_stats <- summary_actor(netifygvcbp)

#Rename columns to get
colnames(actor_stats)[colnames(actor_stats) == "strength_sum_out"] <- "GVC Backward"

colnames(actor_stats)[colnames(actor_stats) == "strength_sum_in"] <- "GVC Backward Imp"


colnames(actor_stats)[colnames(actor_stats) == "network_share_total"] <- "GVC share"


figure2b <- plot_actor_stats(
  actor_stats, 
  across_actor = FALSE, 
  specific_actors = selected,
  specific_stats = c('GVC Backward'),
  multilayer = FALSE
  
)

figure2b


ggsave("Images/Figure 2B.jpg",
       plot = figure2b,
       dpi = 300,
       width = 4,
       height = 3.19,
       units = "in",
       device = "jpeg")


###########################################################################
#                                                                         #
#                          FIGURE 3A and 3B                               #   
#   USA ego network of GVC flows and vote alignment in issues the US      #
#                 deems important (2000 and 2015)                         #  
#                                                                         #
###########################################################################


countries <- c('CHN', 'NLD','FRA','RUS','MEX','CAN','GBR','ESP', 'BRA','IND','COL','ZAF',
               'DEU','AUS','BEL','JPN','IDN','ITA','SAU', 'USA')

dataegousa <- data

dataegousa$gvc[!(dataegousa$imp == "USA" | dataegousa$exp == "USA") ] <- 0

dataegousa <- dataegousa[dataegousa$exp %in% countries & dataegousa$imp %in% countries, ]
dataegousa <- dataegousa[dataegousa$t %in% c(1999, 2014), ]

netifyegousa <- netify(dataegousa, actor1='exp', actor2 ='imp',weight='gvc', time='t', symmetric=FALSE,
                       actor_time_uniform = TRUE, diag_to_NA = TRUE,missing_to_zero = TRUE,
                       nodal_vars = c('gtrade','gtrade_fin','gtrade_int','traditional_trade','traditional_trade_int','traditional_trade_fin',
                                      'gvc','gvcbp','gvcfp','gvcmix',"alignment_us_important","alignment_us_ch_different" ))

netifyegousa = add_nodal(
  netifyegousa,
  summary_actor(netifygvc),
  actor='actor',
  time='time'
)

igraphegousa <- prep_for_igraph(netifyegousa)

fixed_layout <- layout_with_fr(igraphegousa[[2]])

for (i in 1:2) {
  V(igraphegousa[[i]])$x <- fixed_layout[,1]
  V(igraphegousa[[i]])$y <- fixed_layout[,2]
}


plot_graph <- function(graph, title) {
  ggraph(graph, layout = "manual", x = V(graph)$x, y = V(graph)$y) +
    geom_edge_link(aes(linewidth = weight), 
                   color = "lightgray", 
                   arrow = arrow(length = unit(0, "cm")), 
                   show.legend = FALSE) +   # Suppress edge legend here
    geom_node_point(aes(size = network_share_total * 3, fill = alignment_us_important),
                    shape = 21, color = "black", show.legend = c(size = FALSE)) +
    geom_node_text(aes(label = name), vjust = 1.5, size = 3) +
    
    scale_fill_gradientn(colors = c("red", "yellow", "green"), name = "Alignment State Dept") +
    scale_linewidth(range = c(0.5, 2)) +
    
    theme_void() +
    ggtitle(title) +
    theme(legend.position = "bottom")
}


figure3a <- plot_graph(igraphegousa[[1]], title="1999")

figure3a

ggsave("Images/Figure 3A.jpg",
       plot = plot_graph(igraphegousa[[1]], title = "1999"),
       dpi = 300,
       width = 4,
       height = 4,
       units = "in",
       device = "jpeg")




figure3b <- plot_graph(igraphegousa[[2]], title="2014")

figure3b

ggsave("Images/Figure 3B.jpg",
       plot = plot_graph(igraphegousa[[2]], title = "2014"),
       dpi = 300,
       width = 4,
       height = 4,
       units = "in",
       device = "jpeg")






###########################################################################
#                                                                         #
#                              FIGURE 4                                   #   
#   . GVC network and vote alignment in issues the US deems important     #
#                          (2000 - 2015)                                  #  
#                                                                         #
###########################################################################



years <- c(1,6,11,16,21,26)
len <- length(years)

regions <- c('USA', 'China', 'E Asia', 'E EUR', 'SE Asia', 'Latam', 'S Asia', 'ME', 'N America', 'W EUR', 'Africa')

regiondata <- subset_netlet(netifyregion, when_to_subset = years, what_rows_to_subset = regions, what_cols_to_subset = regions )

regiondata <- add_nodal(
  regiondata,
  summary_actor(regiondata),
  actor='actor', time='time'
)

regiondata <- add_nodal(
  regiondata,
  data,
  actor='regiona', time='t'
)

figure4 <- plot(regiondata,
                            edge_color='black',
                            point_color_var = 'region_alignment_US_important_votes',
                            point_size_var='strength_sum_total',
                            layout ='nicely', 
                            static_actor_positions=TRUE,
                            which_static =len,
                            remove_isolates=TRUE,
                            add_label=FALSE,
                            add_text=TRUE)

figure4 <- figure4 + scale_color_gradientn(
  colours = c("red", "yellow", "green"),
  aesthetics = "colour_ggnewscale_2"
)


figure4

ggsave("Images/Figure 4.jpg",
       plot = figure4,
       dpi = 300,
       width = 6.68,
       height = 4.41,
       units = "in",
       device = "jpeg")

